package com.wjyf.reducing_bento.utils;

import com.github.benmanes.caffeine.cache.Cache;
import com.google.common.util.concurrent.RateLimiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: lrk
 * @Date: 2023/3/31 下午 1:40
 * @Description:
 */
@Component
public class CaffeineUtils {

    @Resource
    private Cache<String, String> codeCache;

    @Resource
    private Cache<String, RateLimiter> ipCache;

    public void putCode(String key, String value) {
        codeCache.put(key, value);
    }

    public String getCode(String key) {
        String value = codeCache.getIfPresent(key);
        return value;
    }

    public void putIp(String key) {
        ipCache.put(key, RateLimiter.create(1));
    }

    public RateLimiter getIp(String key) {
        RateLimiter value = ipCache.getIfPresent(key);
        return value;
    }

}
